//
//  WWYSearchViewController.m
//  Pianke
//
//  Created by Daydream on 15/12/31.
//  Copyright © 2015年 dada. All rights reserved.
//

#import "WWYSearchViewController.h"
#import "WWYSearchTableViewCell.h"
#import "WWYSearchModel.h"
#import "LDJ_HomeDetailViewController.h"
#import "HJJPlayerViewController.h"
#import "HJJRadioRootModel.h"



@interface WWYSearchViewController ()<UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UISearchBar *searchBar;

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *allArray;

@property (nonatomic, strong) NSMutableArray *cellArray;

@property (nonatomic, strong) NSMutableArray *searchArray;

@end

@implementation WWYSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    //搜索栏
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH * 2 / 4, 66)];
    self.searchBar.delegate = self;
    self.navigationItem.titleView = self.searchBar;
    self.searchBar.placeholder = @"搜索";
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancelAction:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor darkGrayColor];
    
    //数据请求
    [self getData];
    
    //创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.rowHeight = (SCREEN_HEIGHT - 130) / 9;
    //注册
    [self.tableView registerClass:[WWYSearchTableViewCell class] forCellReuseIdentifier:@"WWYSearchTableViewCell"];
//    [self.tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:@"head"];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(@66);
        make.right.left.mas_equalTo(@0);
        make.height.mas_equalTo(self.view.frame.size.height - 66);
    }];
    
    //tableView适配代码
    self.automaticallyAdjustsScrollViewInsets=NO;
}

- (void)getData{
    //接口：http://api2.pianke.me/search/hotlist
    //body体:  client=2&auth=B8Y4En3h8f1DC8Z8GcHulTJxQMOuNLCNojtsUT0xcWP22NVFSh9U6fo
    //cookie:PHPSESSID=58ts41v1nohdn89damknm0vdt5
    
    //数据初始化
    self.allArray = [NSMutableArray array];
    self.cellArray = [NSMutableArray array];
    
    [LDJ_AppTools postUrl:@"http://api2.pianke.me/search/hotlist" body:@"http://api2.pianke.me/search/hotlist" result:LDJJSON requestStyle:LDJRequestString headerFile:@{@"PHPSESSID":@"58ts41v1nohdn89damknm0vdt5"} success:^(id result) {
        NSLog(@"%@", result);
        for (NSMutableDictionary *temp in [result valueForKey:@"data"]){
           WWYSearchModel *hehe = [WWYSearchModel initWithDictionary:temp];
            [self.cellArray addObject:hehe];
            [self.allArray addObject:temp];
            
            //帅醒
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"WWYSearchTableViewCell";
    WWYSearchTableViewCell *hehe = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    //走model的set方法
    hehe.model = self.cellArray[indexPath.row];
    
    return hehe;
}

#pragma mark- 区头部分..谨记加高度
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    NSLog(@"呵呵呵呵呵呵大大大大大");
//    NSString *identitfer = @"head";
//    UIView *head = [tableView dequeueReusableHeaderFooterViewWithIdentifier:identitfer];
//    if(! head ){
       UIView *head = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 50)];
    head.backgroundColor = [UIColor whiteColor];
        UILabel *recommend = [[UILabel alloc] init];
        recommend.font = [UIFont fontWithName:@"Helvetica-Bold" size:20];
        [head addSubview:recommend];
        [recommend mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(UIEdgeInsetsMake(0, 10, 0, 0));
        }];
        recommend.text = @"他们正在推荐";
    //}
    return head;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50;
}

#pragma mark- 点击cell跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WWYSearchModel *hehe = self.cellArray[indexPath.row];
    //数组形式传入
    NSMutableArray *array = [NSMutableArray array];
    
    for (NSMutableDictionary *listDic in self.allArray) {
        if ([[listDic valueForKey:@"type"]  isEqual: @17]) {
            HJJRadioRootModel *radio = [[HJJRadioRootModel alloc] init];
            [radio setValuesForKeysWithDictionary:listDic];
            radio.tingId = [listDic objectForKey:@"tingid"];
            radio.playSharetext = [[listDic objectForKey:@"playInfo"] objectForKey:@"sharetext"];
            radio.playShareurl = [[listDic objectForKey:@"playInfo"] objectForKey:@"shareurl"];
            radio.webview_url = [[listDic objectForKey:@"playInfo"] objectForKey:@"webview_url"];
            radio.uname = [[[listDic objectForKey:@"playInfo"] objectForKey:@"authorinfo"] objectForKey:@"uname"];
            radio.musicUrl = [[listDic objectForKey:@"playInfo" ]   objectForKey:@"musicUrl"];
            radio.coverimg = [[listDic objectForKey:@"playInfo" ] objectForKey:@"imgUrl"];
            [array addObject:radio];

        }
    }
    
    if (hehe.type == 1){
        LDJ_HomeDetailViewController *home = [[LDJ_HomeDetailViewController alloc] init];
        home.detaId = hehe.contentid;
        [self.navigationController pushViewController:home animated:YES];
    }else if(hehe.type == 17){
        [HJJPlayerViewController sharePlayerViewController].listArray = array;
//        [HJJPlayerViewController sharePlayerViewController].indexRow = 
        //处理过的数组传入
        [self.navigationController pushViewController:[HJJPlayerViewController sharePlayerViewController] animated:YES];
        
    }else{//type24
        LDJ_HomeDetailViewController *home = [[LDJ_HomeDetailViewController alloc] init];
        home.detaId = hehe.contentid;
        [self.navigationController pushViewController:home animated:YES];

    }
}

//点击键盘上的搜索
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
//    self.searchArray = [NSMutableArray array];
//    NSString *string = searchBar.text;
//    NSString *url = @"http://api2.pianke.me/search/suggest";
//    NSString *body = [NSString stringWithFormat:@"auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&keyword=%@&version=3.0.6", string];
//    NSLog(@"body %@", body);
//    //auth=&client=1&deviceid=0F79DA7D-8DF2-416D-B3C3-7BD488A50BDD&keyword=Fe&version=3.0.6
//    [LDJ_AppTools postUrl:url body:body result:LDJJSON requestStyle:LDJRequestString headerFile:nil success:^(id result) {
//        NSLog(@"search result%@", result);
//        for (NSMutableDictionary *temp in [result valueForKey:@"data"]) {
//            
//            
//            
//            [self.searchArray addObject:temp];
//        }
//        
//    } failure:^(NSError *error) {
//        NSLog(@"search error%@", error);
//    }];
}

- (void)cancelAction:(UIBarButtonItem *)item{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
